﻿using System;
using System.IO;
using System.Text;

namespace RG.Common
{
    public static class DebugHelper
    {
        public static string DumpWcfObj(object obj, string FileName = null)
        {
            if (string.IsNullOrEmpty(FileName))
            {
                string baseFileName = string.Format(@"{0}\{1:yyyy_MM_dd__HH_mm_ss}__{2}",
                    Path.GetTempPath(), DateTime.Now, obj.GetType().Name);
                FileName = baseFileName + ".xml";
                int i = 0;
                while (File.Exists(FileName))
                {
                    FileName = baseFileName + "[" + (++i) + "].xml";
                }
            }

            using (var file = new FileStream(FileName, FileMode.Create))
            {
                var xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + obj.ToWcfString(true);
                var buffer = Encoding.UTF8.GetBytes(xml);
                file.Write(buffer, 0, buffer.Length);
            }

            return FileName;
        }
    }
}
